/**
 * Created by wenchuan on 2018/5/3.
 */
define(function () {

    var self,
        app = win.app,
        detailData,
        characteristics = [],
        selectedCharacter = [],
        comments = [],
        other = {
            GWAM016: '',
            GWAM017: ''
        },
        refreshData = {
            key: 0, //评价key
            status: 0,
            count: 0,
            count1: 0,
            count2: 0,
            count3: 0,
            pv: 0
        },
        pageParameters,
        pageUi,
        isUpdate = false
    ;

    var ClassesInfo = app.Module.extend({

        init: function () {
            self = this;
            self.data = {};
            pageParameters = app.pageParameters['classesinfo_page'];
            detailData = pageParameters.item;
            Vue.set(detailData, "GWCM003", detailData.GWCM003);
            // 暂存单进入
            if (pageParameters.type == 'order-temp') {
                isUpdate = true;
                detailData.GWAP100 = detailData.GWCM004;//单价
                detailData.GWAM001_ = detailData.GWCM001;//物料码
                detailData.GWAM005 = 1;
            } else if (pageParameters.last && pageParameters.last.type == 'paidan') {
                // 指派订单--添加进入
                detailData.GWCM003 = 1;
                detailData.GWAM001_ = detailData.GWAM001;
                detailData.GWAM005 = 0;
            } else {
                detailData.GWCM003 = 1;
                detailData.GWAM001_ = detailData.GWAM001;
            }
            self._super({}, win);// 放在后边
        },
        saveData: function () {
            var _data = {
                GWCM001: detailData.GWAM001_,
                GWCM003: detailData.GWCM003,
                GWCM002: detailData.GWAM008,
                GWCM004: detailData.GWAP100 || 0,
                GWCM050: detailData.GWCM050,
                characteristics: []
            };
            // characteristics.forEach(function (v) {
            //     _data.characteristics.push({
            //         GWCN002: v.GWAN002,
            //         GWCN003: v.selectVal
            //     });
            // });
            app.ajax({
                xurl: "api/wx/gwat100/saveData",
                loading: true,
                errmsg: '暂存操作异常',
                xdata: {
                    obj: app.jsonString(_data)
                },
                cb: function (result) {
                    if (result.ok) {
                        app.alert({
                            msg: '暂存成功！',
                            ok: function () {
                                app.back(1);
                            }
                        });
                    }
                }
            });
        },
        subData: function () {
            var _data = {
                GWCM001: detailData.GWAM001_,
                GWCM003: detailData.GWCM003,
                GWCM002: detailData.GWAM008,
                GWCM004: detailData.GWAP100,
                GWCM050: detailData.GWCM050,
                characteristics: []
            };
            // characteristics.forEach(function (v) {
            //     _data.characteristics.push({
            //         GWCN002: v.GWAN002,
            //         GWCN003: v.selectVal
            //     });
            // });
            app.ajax({
                xurl: "api/wx/gwat100/saveData",
                loading: true,
                errmsg: '提交操作异常',
                xdata: {
                    obj: app.jsonString(_data)
                },
                cb: function (result) {
                    if (result.ok) {
                        _data.GWCMDOCNO = result.data.GWCMDOCNO;
                        _data.GWCMSEQ = 1;
                        _data.GWAML003 = detailData.GWAML003;
                        _data.GWAM015 = detailData.GWAM015;
                        app.openPage({
                            src: "yxsjxd/order-submit.html",
                            argsKey: 'ordersubmit_page',
                            args: {
                                items: [_data],
                                cb: function () {
                                    app.back(1);
                                }
                            }
                        });
                    }
                }
            });
        },
        updateData: function () {
            var _data = {
                GWCMDOCNO: detailData.GWCMDOCNO,
                GWCMSEQ: detailData.GWCMSEQ,
                GWCM001: detailData.GWAM001_,
                GWCM003: detailData.GWCM003,
                GWCM004: detailData.GWAP100,
                GWCM050: detailData.GWCM050,
                // characteristics: selectedCharacter
            };
            // characteristics.forEach(function (v) {
            //     _data.characteristics.filterKV({
            //         GWCN002: v.GWAN002
            //     }).addMap({
            //         GWCN003: v.selectVal
            //     }, true);
            // });
            app.ajax({
                xurl: "api/wx/gwat100/updateData",
                loading: true,
                errmsg: '保存操作异常',
                xdata: {
                    obj: app.jsonString({orders: [_data]})
                },
                cb: function (result) {
                    if (result.ok) {
                        app.alert({
                            msg: '商品信息保存成功！',
                            cb: function () {
                                app.back(1);
                            }
                        });
                    }
                }
            });
        },
        addGwcmData: function () {//派遣订单添加
            var _data = app.extend(pageParameters.last.orderInfo, {
                GWCM001: detailData.GWAM001_,
                GWCM002: detailData.GWAM008,
                GWCM003: detailData.GWCM003,
                GWCM050: detailData.GWCM050,
                GWCM004: detailData.GWAP100,
                characteristics: []
            });
            // characteristics.forEach(function (v) {
            //     _data.characteristics.push({
            //         GWCN002: v.GWAN002,
            //         GWCN003: v.selectVal
            //     });
            // });
            app.ajax({
                xurl: "api/wx/gwat200/addGwcmData",
                loading: true,
                errmsg: '保存操作异常',
                xdata: {
                    obj: app.jsonString(_data)
                },
                cb: function (result) {
                    if (result.ok) {
                        app.alert({
                            msg: '订单保存成功',
                            ok: function () {
                                var order = result.data.order;
                                order.GWAML003 = detailData.GWAML003;
                                order.GWAM015 = detailData.GWAM015;
                                order.ischecked = '1';
                                pageParameters.last.cb(order);
                            }
                        });
                    }
                }
            });
        },
        getSelectedCharacteristic: function () {
            app.ajax({
                xurl: "api/wx/gwat100/getSelectedCharacteristic",
                xdata: {
                    GWCNDOCNO: detailData.GWCMDOCNO,
                    GWCNSEQ2: detailData.GWCMSEQ
                },
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        selectedCharacter.pushArray(datas, true);

                        selectedCharacter.forEach(function (v) {
                            characteristics.filterKV({
                                GWAN002: v.GWCN002
                            }).addMap({
                                selectVal: v.GWCN003
                            }, true);
                        });
                    }
                }
            });
        },
        getCharacteristic: function () {
            app.ajax({
                xurl: "api/wx/gwat100/getCharacteristic",
                ocache: false,
                xdata: {
                    GWAN001: detailData.GWAM001
                },
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        datas.forEach(function (v) {
                            if (v.child && v.child.length > 0) {
                                v.selectVal = v.child[0].GWAJ002;
                            }

                        });
                        characteristics.pushArray(datas);
                    }
                    if (pageParameters.type == 'order-temp') {
                        self.getSelectedCharacteristic();
                    }
                    // 规格详情
                    self.getGwamOther();
                }
            });
        },
        getGwcwCount: function () {
            app.ajax({
                xurl: "api/wx/gwat100/getGwcwCount",
                xdata: {
                    GWCW020: detailData.GWAM001_,
                    GWAM005: 1, //料号类型
                    status: refreshData.status // 0全部 1当前商品评论用于子料号
                },
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data.count;
                        app.extend(refreshData, result.data.count);
                        if(data.count>0){
                            refreshData.pv = (refreshData.count1 / refreshData.count*100).toFixed(2);
                        }else{
                            refreshData.pv = 0;
                        }
                    }
                }
            });
        },
        getGwamOther: function () {
            app.ajax({
                xurl: "api/wx/gwat100/getGwamOther",
                xdata: {
                    GWAM001: detailData.GWAM001_
                },
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data.data;
                        app.extend(other, data);
                    }
                }
            });
        },
        _initRefresh: function () { // 实例化刷新组件
            if (self._refreshIds.length == 0) {
                self._refreshIds = ['scroll3'];
                self._refreshVueData = refreshData;
                self._refreshVueMethods = {
                    statusChange: function () {
                        self._refresh();
                        self.getGwcwCount();
                    },
                    getpj: function (val) {
                        refreshData.key = val;
                        self._refresh();
                    }
                };
                this._super(win);
            }else{
                self._refresh();
            }

            self.getGwcwCount();
        },
        _refreshCb: function (goods) {// 刷新组件回调函授
            var _super = this._super;
            app.ajax({
                xurl: "api/wx/gwat100/getGwcw",
                ocache:true,
                xdata: {
                    page:goods.page+1,
                    limit: goods.limit,
                    GWCW020: detailData.GWAM001_,
                    key: refreshData.key,//评论级别
                    GWAM005: 1, //子料号类型
                    status: refreshData.status // 0全部 1当前商品评论用于子料号
                },
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        _super({
                            obj: goods,
                            data: datas,
                            nodata: '暂无评论信息...',
                            nodata_img: 'noneComment.png'
                        });
                    }
                }
            });
        },
        _lazy: function () {

            //self.getCharacteristic();

            // 规格详情
            self.getGwamOther();

            new Vue({
                el: app.getByid(doc, 'scroll2'),
                data: other,
                methods: {
                    provinceChange: function () {

                    }
                }
            });

            $('.wf-infoTab ul li').on('tap', function () {
                $(this).addClass('active').siblings('li').removeClass('active');
                $(this).parents('.wf-infoTab').find('.tabCont .group').eq($(this).index()).show().siblings('.group').hide();
            });

            doc.getElementById('slider').addEventListener('slide', function (e) {
                if (e.detail.slideNumber == 2) {
                    self._initRefresh(win);
                }
            });

            app.taps.call(win, [{
                o: 'btnSubmit',
                cb: function (e) {
                    self.subData();
                },
                confirm: {
                    msg: '确定提交商品？'
                }
            }, {
                o: 'btnSave',
                cb: function (e) {
                    self.saveData();
                },
                confirm: {
                    msg: '确定暂存商品？'
                }
            }, {
                o: 'btnSave1',
                cb: function (e) {
                    if (isUpdate) {
                        self.updateData();
                    } else {
                        self.addGwcmData();
                    }
                },
                confirm: {
                    msg: '确定保存商品？'
                }
            }]);
        },
        _ready: function () {
            var scroll1Vue = new Vue({
                el: app.getByid(doc, 'scroll1'),
                data: {
                    item: detailData,
                    characteristics: characteristics,
                    comments: comments
                },
                methods: {
                    minus: function (item) {
                        if (item.GWCM003 > 1) {
                            item.GWCM003--;
                        }
                    },
                    plus: function (item) {
                        item.GWCM003++;
                    }
                }
            });

            Vue.component('commentsitem', {
                template: app.getByid(doc, 'comments-template'),
                props: {
                    model: Array
                },
                methods: {}
            });
            pageUi = {
                btnSubmit: $('#btnSubmit'), btnSave: $('#btnSave'), btnSave1: $('#btnSave1')
            };
            if ((pageParameters.last && pageParameters.last.type == 'paidan')
                || isUpdate) {
                pageUi.btnSubmit.hide();
                pageUi.btnSave.hide();
                pageUi.btnSave1.show();
            } else {
                pageUi.btnSubmit.show();
                pageUi.btnSave.show();
            }

            app.getCharacteristic({
                gwan001_: detailData.GWAM001_,
                gwam005_: detailData.GWAM005,
                _vue: scroll1Vue,
                cb: function (result) {
                    //用到缓存 会调用2次，不要放跟特征值无关的逻辑

                },
                changeCb: function (obj) {
                    detailData.GWAM001_ = obj.GWAM001;
                    detailData.GWAML003 = obj.GWAML003;
                    detailData.GWAM008_ = obj.GWAM008;//销售单位
                    detailData.GWAP100 = Number(obj.GWAP100);//单价
                    detailData.GWAM015 = obj.GWAM015;

                    // 规格详情
                    self.getGwamOther();
                }
            });

        }
    });

    new ClassesInfo();

});